/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.utils.UtilDevice;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.NiudanSKU;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.ui.activity.NDNiudanActivity;
import com.simon.mengkou.ui.fragment.NDPlayFragment;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/1/20.
 * desc    : 扭蛋成功对话框
 */
public class PlayResultDialog extends Dialog {
    private NDNiudanActivity mContext;
    private NDPlayFragment mFragment;

    //gif动画
    @Bind(R.id.result_id_gif) SimpleDraweeView mSdvGif;
    @Bind(R.id.result_id_root) LinearLayout mLlRoot;
    //
    @Bind(R.id.result_id_image) SimpleDraweeView mSdvImage;
    //
    @Bind(R.id.result_id_text) TextView mTvText;

    private NiudanSKU mSku;

    public PlayResultDialog(NDNiudanActivity context, NDPlayFragment fragment, NiudanSKU sku) {
        super(context, R.style.res_theme_dialog_waiting);
        mContext = context;
        mFragment = fragment;
        mSku = sku;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_play_result);
        ButterKnife.bind(this);
        //gif
        OuerApplication.mImageLoader.loadGifRes(mSdvGif, R.drawable.play_ic_result);
        //
        if(mSku.getImage() != null) {
            OuerApplication.mImageLoader.loadCircleImage(mSdvImage, mSku.getImage().getUrl());
        }
        //
        mTvText.setText(Html.fromHtml(mContext.getString(R.string.play_result_msg, mSku.getName())));

        setCanceledOnTouchOutside(true);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mSdvGif.setVisibility(View.GONE);
                mLlRoot.setVisibility(View.VISIBLE);
            }
        }, 1500);
    }

    /**
     * 保存图鉴
     */
    @OnClick(R.id.result_id_save)
    protected void save() {
        cancel();
        UtilOuer.toast(mContext, R.string.play_result_add_img);
    }

    /**
     * 寄送
     */
    @OnClick(R.id.result_id_send)
    protected void send() {
        cancel();
        mFragment.moekouMy();
    }

    @Override
    public void show() {
        super.show();

        Window window = getWindow();
        // 获取对话框当前的参数值
        WindowManager.LayoutParams params = window.getAttributes();
        //高度
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //宽度设置为屏幕的0.9
        params.width = (int) (UtilDevice.getDevice(mContext).getWidth() * 0.8);
        window.setAttributes(params);
    }

}
